﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLLPC;
using DLLPC.Utilities;

namespace VisionCore
{
    public static class RTCHelper
    {
        public static EventHelper_RTC IOEvent = new EventHelper_RTC();
        public static IOMap_RTC DIO = new IOMap_RTC();
        public static RTC_UDP_Client RTC_COM;

        public static double GetPollingTime
        {
            get
            {
                return RTC_COM.CycleTime;
            }
        }

        public static void Start()
        {
            IOEvent.Start();

            //RTC_COM = new RTC_UDP_Client(ParameterHelper.SysParam.COM.RTCServer_IP,ParameterHelper.SysParam.COM.A_Port,(byte)ParameterHelper.SysParam.COM.A_RemoteDB,0,512,(byte)ParameterHelper.SysParam.COM.A_LocalDB,0,512);
            RTC_COM.ChannelName = "MotionPC_COM";
            RTC_COM.DataArrive += new EventHandler(UpdateRemoteData);
            RTC_COM.DataSend += new EventHandler(UpdateLocalData);
            RTC_COM.Start();
        }

        public static void Quit()
        {
            RTC_COM.Quit();
            IOEvent.Quit();
        }

        private static void UpdateRemoteData(object sender, EventArgs e) 
        {
            RTC_UDP_Client ch = sender as RTC_UDP_Client;
            if (ch != null)
            {
                
                IOEvent.UpdateRemoteFlag(ch.RemoteToLocal);
                DIO.UpdateRemote(ch.RemoteToLocal);
            }
        }

        private static void UpdateLocalData(object sender, EventArgs e)
        {
            RTC_UDP_Client ch = sender as RTC_UDP_Client;
            if (ch != null)
            {
                DIO.UpdateLocal(ref ch.LocalToRemote);
                IOEvent.UpdateLocalFlag(ref ch.LocalToRemote);
            }
        }
    }
}
